package com.example.demo.leetcode.ali;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class 多线程轮流输出个数 {

    static ReentrantLock lock = new ReentrantLock();
    static volatile int a = 1;

    public static void main(String[] args) throws InterruptedException {

        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();

        List<Condition> conditions = Lists.newArrayList(condition1,condition2,condition3);

        Thread t1 = new Thread(new MyThread(condition1, condition2, conditions));
        Thread t2 = new Thread(new MyThread(condition2, condition3, conditions));
        Thread t3 = new Thread(new MyThread(condition3, condition1, conditions));
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(1000);
        lock.lock();
        condition1.signal();
        lock.unlock();
        t1.join();
        t2.join();
        t3.join();

    }

    /**
     * 这种写法 会出现死锁
     * 抢到锁，才能进行await，没抢到锁，就进入不了await,那么signal就会失效，导致死锁
     */
    static class MyThread implements Runnable {
        Condition self;
        Condition next;
        List<Condition> conditions;
        public MyThread(Condition start, Condition end, List<Condition> conditions) {
            this.self = start;
            this.next = end;
            this.conditions = conditions;
        }

        @Override public void run() {
            while (a < 100000) {
                System.out.println(Thread.currentThread().getName()+":lock start");
                lock.lock();
                System.out.println(Thread.currentThread().getName()+":lock success");
                try {
                    String name = Thread.currentThread().getName();
                    if(a >= 100000){
                        for(Condition condition : conditions){
                            condition.signal();
                        }
                        System.out.println(name + "=====================end");
                        return;
                    }
                    System.out.println(Thread.currentThread().getName()+":self.await start");
                    self.await();
                    System.out.println(Thread.currentThread().getName()+":self.await success");

                    if(a >= 100000){
                        for(Condition condition : conditions){
                            condition.signal();
                        }
                        System.out.println(name + "=====================end");
                        return;
                    }
                    a = a + 1;
                    System.out.println(name + ":" + a);
                    next.signal();
                    System.out.println(Thread.currentThread().getName()+":next.signal");
                    lock.unlock();
                    System.out.println(Thread.currentThread().getName()+":unlock");
                    if(a >= 100000){
                        for(Condition condition : conditions){
                            condition.signal();
                        }
                        System.out.println(name + "=====================end");
                        return;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 避免死锁写法,最后进行await，一定会进入等待状态 等待唤醒。
     *  轮流执行原则:
     *  1.在一个临界区内，await一定要放到最后去执行
     *  2.函数执行前和await前，一定要判断是否退出
     *  3.如果要退出，在退出前，一定要唤醒其他线程
     */
    static class MyThread2 implements Runnable {
        Condition self;
        Condition next;
        boolean fisrt = true;

        List<Condition> conditions;
        public MyThread2(Condition start, Condition end, List<Condition> conditions) {
            this.self = start;
            this.next = end;
            this.conditions = conditions;
        }

        @Override public void run() {
            while (a < 100000) {
                lock.lock();
                try {
                    if(fisrt){
                        self.await();
                        fisrt = false;
                    }
                    String name = Thread.currentThread().getName();
                    if(a >= 100000){
                        for(Condition condition : conditions){
                            condition.signal();
                        }
                        System.out.println(name + "=====================end");
                        return;
                    }
                    a = a + 1;
                    System.out.println(name + ":" + a);
                    next.signal();

                    if(a >= 100000){
                        System.out.println(name + "=====================end");
                        for(Condition condition : conditions){
                            condition.signal();
                        }
                        return;
                    }
                    self.await();

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
            String name = Thread.currentThread().getName();
            System.out.println(name + "=====================end");
        }
    }

}
